package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.MarketAdService;
import com.cskaoyan.market.service.impl.MarketAdServiceImpl;
import com.cskaoyan.market.util.GetRequestBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/admin/ad/*")
public class AdminAdController extends HttpServlet {
    private MarketAdService marketAdService = new MarketAdServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = getOpString(req);
        //查询广告接口
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opString = getOpString(req);
        //添加广告接口
        if ("create".equals(opString)) {
            create(req, resp);
        }
        //编辑广告接口
        if ("update".equals(opString)) {
            update(req, resp);
        }
        //删除广告接口
        if ("delete".equals(opString)) {
            delete(req, resp);
        }

    }


    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        //将请求体中Json格式的字符串转换成对应修改条件的广告实体类
        MarketAd ad = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAd.class);
        MarketAd marketAd = marketAdService.update(ad);
        String asString = JacksonUtil.writeValueAsString(ResponseUtil.ok(marketAd));
        resp.getWriter().println(asString);
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        MarketAd marketAd = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAd.class);
        MarketAd InsertedMarketAd = marketAdService.createAd(marketAd);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(InsertedMarketAd));
        resp.getWriter().println(respBody);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
//        将请求体中Json字符串中的id的value解析成字符串
        String id = JacksonUtil.parseString(requestBody, "id");
        marketAdService.deleteAd(id);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok());
        resp.getWriter().println(respBody);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        //name、content不是必须的请求参数，可能为null
        if (name == null) {
            name = "";
        }
        String content = req.getParameter("content");
        if (content == null) {
            content = "";
        }
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketAd> marketAds = marketAdService.list(page, limit, name, content, sort, order);
        Object o = ResponseUtil.okList(marketAds);
        String s = JacksonUtil.writeValueAsString(o);
        resp.getWriter().println(s);

    }

    //分发请求
    private String getOpString(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        return requestURI.replace(req.getContextPath() + "/admin/ad/", "");
    }
}
